package com.foreknow.demo9;

public class UserManager {
    String[] name = new String[2];
    String[] pass = new String[2];

    public void register(String username, String password) throws CustomException {
        //查看字符串的长度
        int nameLength = username.length();
        int passLength = password.length();

        //校验用户名和密码是否为空
        if (username == null || password == null) {
            //抛出异常
            throw new CustomException("用户名或密码为空！");
        }
        //判断用户名nameLength的长度
        if (nameLength < 6 || nameLength > 14) {
            throw new CustomException("用户名长度不符合规范！");
        }
        //判断密码passLength的长度
        if (passLength < 8 || passLength > 16) {
            throw new CustomException("密码长度不符合规范！");
        }

        for (int i = 0; i < name.length; i++) {
            if (name[i] == null && pass[i] == null) {
                name[i] = username;
                pass[i] = password;
                System.out.println("注册成功！");
                return;
            }
        }
        throw new CustomException("用户已达到上限，无法注册");
    }

    public static void main(String[] args) {
        UserManager userManager = new UserManager();
        UserManager userManager1 = new UserManager();
        try {
            userManager.register("zhangsan", "zhangsan");
            userManager.register("lisiji", "lisiji44");
            userManager.register("lisij", "lisiji44");
        } catch (CustomException e) {
            e.printStackTrace();
            try {
                userManager.register("lisiji", "lisiji44");
            } catch (CustomException ex) {
                ex.printStackTrace();
            }
        }
    }
}
